layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    /**
     * 报告提交表管理
     */
    var TCommitReport = {
        tableId: "tCommitReportTable"
    };

    /**
     * 初始化表格的列
     */
    TCommitReport.initColumn = function () {
        return [[
            {field: 'companyName', sort: true, title: '公司名称'},
            {field: 'isCommit', sort: true, title: '是否提交',templet: function (d) {
                    if(d.isCommit == 2){
                        return '已提交';
                    } else {
                        return  '未提交'
                    }
                }},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TCommitReport.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(TCommitReport.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    TCommitReport.openAddDlg = function () {
        func.open({
            title: '添加报告提交表',
            content: Feng.ctxPath + '/tCommitReport/add',
            tableId: TCommitReport.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    TCommitReport.openEditDlg = function (data) {
        func.open({
            title: '修改报告提交表',
            content: Feng.ctxPath + '/tCommitReport/edit?id=' + data.id,
            tableId: TCommitReport.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    TCommitReport.exportExcel = function () {
        var checkRows = table.checkStatus(TCommitReport.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TCommitReport.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tCommitReport/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(TCommitReport.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TCommitReport.tableId,
        url: Feng.ctxPath + '/tCommitReport/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TCommitReport.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TCommitReport.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TCommitReport.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TCommitReport.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TCommitReport.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TCommitReport.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TCommitReport.onDeleteItem(data);
        }else if(layEvent === "commitReport"){
            TCommitReport.commitReport(data);
        }else if(layEvent === "returnReport"){
            TCommitReport.returnReport(data);
        }else if(layEvent === "downReport"){
            TCommitReport.downReport(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

         TCommitReport.changeDelFlag(id, checked);
    });

    /**
     * 修改状态
     *
     * @param userId 用户id
     * @param checked 是否选中（true,false），选中就是解锁用户，未选中就是锁定用户
     */
     TCommitReport.changeDelFlag = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/tCommitReport/enable", function (data) {
                if (data.code == 200) {
                    Feng.success("启用成功!");
                }else{
                    Feng.error("启用失败!" + data.message + "!");
                }
                table.reload( TCommitReport.tableId);
            }, function (data) {
                Feng.error("启用失败!" + data.message + "!");
                table.reload( TCommitReport.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/tCommitReport/disable", function (data) {
                if(data.code == 200) {
                    Feng.success("禁用成功!");
                }else{
                    Feng.error("禁用失败!" + data.message + "!");
                }
                table.reload( TCommitReport.tableId);
            }, function (data) {
                Feng.error("禁用失败!" + data.message + "!");
                table.reload( TCommitReport.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        }
    };


    /**
     * 点击提交
     *
     * @param data 点击按钮时候的行数据
     */
    TCommitReport.commitReport = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tCommitReport/commitReport", function (data) {
                if (data.code == 200) {
                    Feng.success("提交成功!");
                }else{
                    Feng.error("提交失败!" + data.message + "!");
                }
                table.reload(TCommitReport.tableId);
            }, function (data) {
                Feng.error("提交失败!" + data.message + "!");
            });
            ajax.set("companyId", data.companyId);
            ajax.start();
        };
        Feng.confirm("是否提交?", operation);
    };

    /**
     * 点击退回
     *
     * @param data 点击按钮时候的行数据
     */
    TCommitReport.returnReport = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tCommitReport/returnReport", function (data) {
                if (data.code == 200) {
                    Feng.success("退回成功!");
                }else{
                    Feng.error("退回失败!" + data.message + "!");
                }
                table.reload(TCommitReport.tableId);
            }, function (data) {
                Feng.error("退回失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否退回?", operation);
    };

    /**
     * 点击生成报告
     *
     * @param data 点击按钮时候的行数据
     */
    TCommitReport.downReport = function (data) {
        window.location.href = Feng.ctxPath + "/downWord/exportData?companyId="+data.companyId;
        // var operation = function () {
        //
        //     var ajax = new $ax(Feng.ctxPath + "/downWord/downReport", function (data) {
        //         if (data.code != 200) {
        //             Feng.error("生成失败!" + data.message + "!");
        //
        //         }else{
        //             window.location.href = Feng.ctxPath + "/downWord/downReport?companyId="+data.companyId;
        //             Feng.success("生成成功!");
        //         }
        //         table.reload(TCommitReport.tableId);
        //     }, function (data) {
        //         if(data == '' || data == null){
        //             Feng.error("生成失败!" + data.message + "!");
        //         }
        //
        //         Feng.error("生成失败!" + data.message + "!");
        //     });
        //     ajax.set("companyId", data.companyId);
        //     ajax.start();
        // };
        // Feng.confirm("是否生成报告?", operation);
    };
});
